/**
 * RecvService.java
 * 版权所有(C) 2014
 * 创建者:cuiran 2014-9-26 下午5:17:22
 */
package com.ghyf.mplay.service;

import com.ghyf.mplay.net.common.GData;
import com.ghyf.mplay.net.common.RecvBase;
import com.ghyf.mplay.net.common.RecvControlCmd;
import com.ghyf.mplay.net.common.RecvFileCmd;
import com.ghyf.mplay.net.common.RecvHeartBeat;
import com.ghyf.mplay.net.common.RecvInformation;
import com.ghyf.mplay.util.LogUtil;
import com.ghyf.mplay.value.ConstantValue;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;

/**
 *  此服务用于启动UDP Server
 * @author cuiran
 * @version 1.0.0
 */
public class RecvService extends Service {
	private static final String TAG="RecvService";
	private Runnable runnable_heartbeat;
	private Thread thread_heartbeat;

	private Runnable runnable_control;
	private Thread thread_control;
	
	private Runnable runnable_Information;
	private Thread thread_Information;
	
	private  RecvFileCmd runnable_File;
	private Thread thread_File;
	
	/* (non-Javadoc)
	 * @see android.app.Service#onBind(android.content.Intent)
	 */
	@Override
	public IBinder onBind(Intent arg0) {
		
		return null;
	}

	/* (non-Javadoc)
	 * @see android.app.Service#onCreate()
	 */
	@Override
	public void onCreate() {
		super.onCreate();
		
		startServer();
		
		IntentFilter myIntentFilter = new IntentFilter();
		myIntentFilter.addAction(RecvBase.ACTION_CMD_SEND);
		//注册广播      
		registerReceiver(sendReceiver, myIntentFilter);
	}

	/**
	 * 发送下载进度给控制中心
	 */
	 private final BroadcastReceiver sendReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			
			 int progress=intent.getIntExtra(ConstantValue.EXTRA_TYPE, 100);
			 LogUtil.i(TAG, " sendReceiver sendReceiver ........---------progress=="+progress);
			 runnable_File.sendProgress(progress);
			 
		}
	 };
	/**
	 * 开启服务
	 */
	private void startServer(){
		runnable_heartbeat = new RecvHeartBeat(GData.heartbeatPort, GData.serverPort,this);
		thread_heartbeat = new Thread(runnable_heartbeat);
		thread_heartbeat.start();

		runnable_control=new RecvControlCmd(GData.controlPort, GData.serverPort,this);
		thread_control = new Thread(runnable_control);
		thread_control.start();

		runnable_Information = new RecvInformation(GData.infoPort, GData.serverPort,this);
		thread_Information = new Thread(runnable_Information);
		thread_Information.start();
		
		runnable_File = new RecvFileCmd(GData.filePort, GData.serverPort,this);
		thread_File = new Thread(runnable_File);
		thread_File.start();
		
		
	}
	/* (non-Javadoc)
	 * @see android.app.Service#onDestroy()
	 */
	@Override
	public void onDestroy() {
		super.onDestroy();
		
		stopServer();
		unregisterReceiver(sendReceiver);
	}

	/**
	 * 停止server<br>
	 * 2014-3-24 下午3:35:02
	 *
	 */
	public void stopServer(){
		if(thread_heartbeat !=null) thread_heartbeat.interrupt();
		if(thread_control !=null) thread_control.interrupt();
		if(thread_Information !=null) thread_Information.interrupt();
		if(thread_File !=null) thread_File.interrupt();
		
		
	}
}

